﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Diplom.NumericArrays;
using Diplom.Graphics.D3D;
using Diplom.Graphics.D2D;
using Diplom.Wavelets;
using System.IO;

namespace Diplom.Forms
{
    public partial class FourierDesigner : Form
    {
        Numeric1DArray numeric1DArray = null;
        Numeric1DArray fourierArray = null;

        public FourierDesigner()
        {
            InitializeComponent();
        }

        public FourierDesigner(ref Numeric1DArray array)
        {
            InitializeComponent();

            if (array != null)
            {
                numeric1DArray = new Numeric1DArray(array);
                //fourierApply_Click(new object(), new EventArgs());
            }
        }

        private void butGenerate_Click(object sender, EventArgs e)
        {
            ArrayDesigner arrayDesigner = new ArrayDesigner();
            if (arrayDesigner.ShowDialog() == DialogResult.OK)
            {
                numeric1DArray = new Numeric1DArray(arrayDesigner.Array);
                numeric1DArray.InitCharacters();
                GC.Collect();

                fourierApply_Click(sender, e);
            }
        }

        private void CustomPaint()
        {
            if (numeric1DArray != null)
            {
                fourierImage.Image = new Bitmap(fourierImage.Width, fourierImage.Height);
                System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(fourierImage.Image);
                graphics.Clear(Color.White);
                DrawStyle drawStyle = new DrawStyle(Color.Blue, new Point(0, 0), new Point(1, 1), Color.Red, Color.Green);

                D2DVisualizer.DrawGraphic(ref graphics, ref numeric1DArray, new RectangleF(new PointF(40, 50), new SizeF(fourierImage.Image.Width - 50, fourierImage.Image.Height / 3 - 100)), drawStyle);
                D2DVisualizer.DrawSignature(ref graphics, ref numeric1DArray, new RectangleF(new PointF(40, 50), new SizeF(fourierImage.Image.Width - 50, fourierImage.Image.Height / 3 - 100)), drawStyle, new Size(25, 10));

                if (fourierArray != null)
                {
                    D2DVisualizer.DrawGraphic(ref graphics, ref fourierArray, new RectangleF(new PointF(40, 25 + fourierImage.Height / 3), new SizeF(fourierImage.Width - 50, fourierImage.Height * 2 / 3 - 50)), drawStyle);
                    
                    D2DVisualizer.DrawSignature(ref graphics, ref fourierArray, new RectangleF(new PointF(40, 25 + fourierImage.Height / 3), new SizeF(fourierImage.Width - 50, fourierImage.Height * 2 / 3 - 50)), drawStyle, new Size(25, 10));
                }
            }
        }

        private void FourierDesigner_ResizeEnd(object sender, EventArgs e)
        {
            this.CustomPaint();
        }

        private void fourierApply_Click(object sender, EventArgs e)
        {
            if (numeric1DArray != null)
            {
                Complex1DArray complex1DArray = Fourier.Transform(ref numeric1DArray);
                fourierArray = Analitics.Periodogramma(ref complex1DArray);
                this.CustomPaint();
            }
        }

        private void save_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.InitialDirectory = Path.GetDirectoryName(Application.ExecutablePath);
            saveFileDialog.Filter = "Jpg files(*.jpg)|*.jpg";

            if (fourierImage == null || fourierImage.Image == null) { return; }
            if (saveFileDialog.ShowDialog() == DialogResult.OK)
            {
                if (String.IsNullOrEmpty(saveFileDialog.FileName.Trim())) { return; }

                fourierImage.Image.Save(saveFileDialog.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                MessageBox.Show("Файл '" + saveFileDialog.FileName + "' сохранен", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (numeric1DArray != null && fourierArray != null)
            {
                Numeric2DArray temp = new Numeric2DArray(0);
                ImageSaver imageSaver = new ImageSaver(ref numeric1DArray, ref temp, ref fourierArray, ImageSaver.SaveType.Fourier);
                imageSaver.ShowDialog();
            }
        }
    }
}
